#include "applyfriendlist.h"

#include <QScrollBar>
#include <QWheelEvent>

ApplyFriendList::ApplyFriendList(QWidget *parent)
    : QListWidget(parent)
{
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    this->viewport()->installEventFilter(this);
}

ApplyFriendList::~ApplyFriendList() {}

bool ApplyFriendList::eventFilter(QObject *watched, QEvent *event)
{
    // 鼠标进入显示滚动条，鼠标离开隐藏滚动条
    if (watched == this->viewport())  // 滚动区域的函数，返回一个指向窗口对象的指针
    {
        if (event->type() == QEvent::Enter) {
            this->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        }
        else if (event->type() == QEvent::Leave) {
            this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        }
    }

    // 鼠标点击事件
    if (watched == this->viewport() && event->type() == QEvent::MouseButtonPress) {
        emit sig_show_search(false);  // 点击关闭搜索框
    }

    // 鼠标滚轮事件
    if (watched == this->viewport() && event->type() == QEvent::Wheel) {
        QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
        int numDegrees = wheelEvent->angleDelta().y() / 8;
        int numSteps = numDegrees / 15;

        // 设置滚动幅度
        this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() - numSteps);

        return true;  // 停止事件传递
    }

    return QListWidget::eventFilter(watched, event);
}
